var api = require("../../../../api.js"),
  app = getApp();
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let dateArr = {
  year: year,
  month: month
};

/**
 * 判断是否闰年
 */
let leapYear = function (Year) {
  if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
    return (true);
  } else { 
    return (false); 
  }
}

/**
 * 判断指定月天数
 */
let countDayByMonth = function (Year, Month){
  if (Month == 1 | Month == 3 | Month == 5 | Month == 7 | Month == 8 | Month == 10 | Month == 12){
    return 31;
  } else if (Month == 4 | Month == 6 | Month == 9 | Month == 11){
    return 30;
  } else if (Month == 2){
    let isLeapYear = leapYear(Year);
    if (isLeapYear){
      return 29;
    }else{
      return 28;
    }
  }
}

/**
 * 获取当月第一天星期几
 */
let getDayByMonth = function (Year, Month){
  return new Date(Year + '-' + Month + '-1').getDay();
}

let currentMonth = countDayByMonth(year,month);
let monthArr = [];
let currentDay = getDayByMonth(year, month);
let i,tempArr = [];

/**
 * 填充默认
 */
for (i = 0; i < currentDay;i++){
  tempArr.push({
    name: "",
  });
}

/**
 * 日期遍历
 */
for (i = 0; i < currentMonth;i++){
  currentDay++;
  tempArr.push({
    name: year + '-' + month + '-' + (i + 1),
    value: i + 1,
    flag: false
  });
  if (currentDay % 7 == 0 || i == currentMonth - 1){
    monthArr.push(tempArr);
    tempArr = [];
  }
}


Page({
  data: {
    dateArr: dateArr,
    monthArr: monthArr,
    isSign: '', // 签到状态
    data:'',
  },
  onLoad: function (option) {
    // 模拟签到
    let _this = this;
    _this.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
      cid: option.cid,
    });
    let _monthArr = _this.data.monthArr;
    // 需要标记的日期
    let isSignInArr = ["2018-5-3", "2018-5-4", "2018-5-8", "2018-5-10", "2018-5-11", "2018-5-12"];
    // 签到标记初始化
    _monthArr.forEach(function(item,index){
      if(item){
        item.forEach(function (item, index){
          isSignInArr.forEach(function (sign, index){
            if (item.name == sign){
              item.flag = true;
            }
          });
        });
      }
    });
    _this.setData({
      monthArr: _monthArr,
    });
  },
  signIn: function () {
    let that = this;
    var uid = wx.getStorageSync('uid');
    var bid = wx.getStorageSync('bid');
    console.log("发送签到请求");
    app.request({
      url: api.member.sign,
      data: { 
        uid: uid,
        bid: bid,
      },
      method: 'post',
      success: function (res) {
        console.log('rerere', res)
        var data = res.data
        that.setData({
          data: data,
        })
      }
    })
    // 重置签到状态
    that.setData({
      isSign: true
    });
  },
  onShow: function (options) {
    var that = this
    var uid = wx.getStorageSync('uid');
    var isSign = '';
    app.request({
      url: api.member.info,
      data: {uid:uid},
      method: 'post',
      success: function (res) {
        console.log('rerere', res.data)
        var data = res.data
        if (data == 1001){
          wx.showModal({
            title: '提示',
            content: '你还不是会员，请先购买会员！',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../member-purchase/index'
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../member-center/index'
                })
              }
            }
          })
        }
        if (res.data.msg){
          isSign = false;
        }else{
          isSign = true;
        }
        that.setData({
          data: data,
          isSign: isSign,
        })
      }
    })
  },
})